﻿using Common.Interfaces;
using Domain.Exceptions;

namespace Domain.ValueObjects
{
    public class NickName : ValueObject
    {
        public string Value { get; }

        private NickName(string value) => Value = value;

        public static NickName Create(string value)
        {
            value = value.Trim();
            if (string.IsNullOrEmpty(value))
            {
                throw new InvalidNickNameException("昵称不能为空");
            }

            if (value.Length > 30)
                throw new InvalidNickNameException("昵称长度应该在1~30之间");
            return new NickName(value);
        }

        protected override IEnumerable<object> GetEqualityComponents()
        {
            yield return Value;
        }

        public static implicit operator string(NickName nickName) => nickName.Value;
    }
}
